package com.yanqu.road.server.manager.suoyao;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.suoyao.SuoYaoBoxData;
import com.yanqu.road.entity.activity.suoyao.SuoYaoNotice;
import com.yanqu.road.entity.activity.suoyao.config.*;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.bussiness.activity.SuoYaoBusiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.pb.SuoYaoPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.SuoYaoActivityProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SuoYaoActivity {

    private static Logger logger = LogManager.getLogger(SuoYaoActivity.class.getName());

    private SuoYaoConfig suoYaoConfig;

    private SuoYaoBoxData boxData;
    private List<SuoYaoNotice> suoYaoNoticeList;

    public SuoYaoActivity(ActivityInfo tmpActivityInfo) {
        // 读配置
        suoYaoConfig = initConfig(tmpActivityInfo);
        // 读数据
        boxData = SuoYaoBusiness.getSuoYaoBoxData(tmpActivityInfo.getActivityId());
        if (boxData == null) {
            SuoYaoBoxData tmpBoxData = new SuoYaoBoxData();
            tmpBoxData.setActivityId(tmpActivityInfo.getActivityId());
            tmpBoxData.setCount(0);
            tmpBoxData.setInsertOption();
            boxData = tmpBoxData;
        }
        suoYaoNoticeList = SuoYaoBusiness.getSuoYaoNoticeList(tmpActivityInfo.getActivityId());
    }

    public void save() {
        if (boxData.isInsertOption()) {
            SuoYaoBusiness.addSuoYaoBoxData(boxData);
        } else if (boxData.isUpdateOption()) {
            SuoYaoBusiness.updateSuoYaoBoxData(boxData);
        }

        int size = suoYaoNoticeList.size();
        for (int i = 0; i < size; i++) {
            SuoYaoNotice suoYaoNotice = suoYaoNoticeList.get(i);
            if (suoYaoNotice.isInsertOption()) {
                SuoYaoBusiness.addSuoYaoNotice(suoYaoNotice);
            }
        }
    }

    /**
     * 抽低价隐藏盲盒
     */
    public boolean drawHideBox() {
        synchronized (boxData) {
            if (boxData.getCount() < suoYaoConfig.getHideBoxCount()) {
                boxData.setCount(boxData.getCount() + 1);
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * 添加公告
     */
    public void uploadNotice(long userId, int rewardId, UserBaseInfo userBaseInfo, int type) {
        SuoYaoNotice suoYaoNotice = new SuoYaoNotice();
        suoYaoNotice.setActivityId(suoYaoConfig.getActivityId());
        suoYaoNotice.setUserId(userId);
        suoYaoNotice.setUserBaseInfo(userBaseInfo);
        suoYaoNotice.setRewardId(rewardId);
        suoYaoNotice.setTime(System.currentTimeMillis());
        suoYaoNotice.setType(type);
        suoYaoNotice.setInsertOption();

        synchronized (suoYaoNoticeList) {
            suoYaoNoticeList.add(suoYaoNotice);
        }

        // 同步到区服
        SuoYaoActivityProto.SyncSuoYaoNoticeRespMsg.Builder builder = SuoYaoActivityProto.SyncSuoYaoNoticeRespMsg.newBuilder();
        builder.setNotice(SuoYaoPb.parseSuoYaoNoticeMsg(suoYaoNotice));
        YanQuMessage yanQuMessage = YanQuMessageUtils.buildMessage(GameProtocol.S_SUOYAO_ACTIVITY_SYNC_NOTICE, builder);
        for (Long serverId : suoYaoConfig.getActivityInfo().getServerIdList()) {
            MessageHelper.sendPacket(serverId, 0, yanQuMessage);
        }
    }

    /**
     * 生成一份配置
     */
    private SuoYaoConfig initConfig(ActivityInfo tmpActivityInfo) {
        SuoYaoConfig tmpConfig = new SuoYaoConfig();
        tmpConfig.setActivityInfo(tmpActivityInfo);
        tmpConfig.setActivityId(tmpActivityInfo.getActivityId());

        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(tmpActivityInfo.getActivityId());

        // 其他配置
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(tmpActivityInfo.getActivityId());

        tmpConfig.setScoreItemId(activityConfigMap.get(eActivityConfigType.SUOYAO_SCORE_ITEM_ID.getName()).getIntValue());

        tmpConfig.setExploreItemId(activityConfigMap.get(eActivityConfigType.SUOYAO_EXPLORE_ITEM_ID.getName()).getIntValue());

//        tmpConfig.setSwordItemId(activityConfigMap.get(eActivityConfigType.SUOYAO_SWORD_ITEM_ID.getName()).getIntValue());

        tmpConfig.setExploreCountList(activityConfigMap.get(eActivityConfigType.SUOYAO_EXPLORE_COST_COUNT_LIST.getName()).getIntListValue(";"));

//        List<Integer> boxCountParam = activityConfigMap.get(eActivityConfigType.SUOYAO_HIDE_BOX_LUCKY_PARAM.getName()).getIntListValue(";");
//        tmpConfig.setHideBoxTimes(boxCountParam.get(0));
//        tmpConfig.setHideBoxCount(boxCountParam.get(1) * tmpActivityInfo.getServerIdList().size());
//        tmpConfig.setLuckyPercent(boxCountParam.get(2));

//        List<Integer> additionParam = activityConfigMap.get(eActivityConfigType.SUOYAO_HIDE_BOX_ADDITION_PARAM.getName()).getIntListValue(";");
//        tmpConfig.setHideBoxAddTimes(additionParam.get(0));
//        if (tmpConfig.getHideBoxAddTimes() <= tmpConfig.getHideBoxTimes()) {
//            tmpConfig.setHideBoxAddTimes(tmpConfig.getHideBoxTimes() + 1);
//            logger.error("SuoYaoActivity config SUOYAO_HIDE_BOX_ADDITION_PARAM error! activityId={}", tmpConfig.getActivityId());
//        }
//        tmpConfig.setHideBoxAddWeight(additionParam.get(1));

//        tmpConfig.setNoticeCount(activityConfigMap.get(eActivityConfigType.SUOYAO_NOTICE_COUNT.getName()).getIntValue());

        // 盲盒奖励
//        List<SuoYaoBoxItemConfig> boxItemConfigList = new ArrayList<>();
//        String boxItemParam = activityConfigMap.get(eActivityConfigType.SUOYAO_BOX_ITEM_PARAM.getName()).getValue();
//        for (String boxItem : StringUtils.stringToStringList(boxItemParam, "\\|")) {
//            List<Integer> tmpList = StringUtils.stringToIntegerList(boxItem, ";");
//            SuoYaoBoxItemConfig boxItemConfig = new SuoYaoBoxItemConfig(tmpList.get(0), tmpList.get(1), tmpList.get(2) == 1);
//            boxItemConfigList.add(boxItemConfig);
//        }
//        tmpConfig.setBoxItemConfigList(boxItemConfigList);

//        tmpConfig.setDrawBoxCost(activityConfigMap.get(eActivityConfigType.SUOYAO_DRAW_BOX_COST.getName()).getValue());

        // 塔的奖励 配置
        Map<Integer, Map<Integer, SuoYaoRewardConfig>> tmpConfigMap = SuoYaoBusiness.getSuoYaoRewardConfigMap(tmpActivityInfo.getActivityId());
        tmpConfig.setFloorMap(tmpConfigMap);

        // 图鉴配置
        List<SuoYaoBookConfig> bookConfigList = SuoYaoBusiness.getSuoYaoBookConfigList(tmpActivityInfo.getActivityId());
        tmpConfig.setBookConfigList(bookConfigList);

        Map<Integer, SuoYaoTopRewardConfig> suoYaoTopRewardConfigMap = SuoYaoBusiness.getSuoYaoTopRewardConfigMap(tmpActivityInfo.getActivityId());
        tmpConfig.setTopRewardConfigMap(suoYaoTopRewardConfigMap);

        Map<Integer, SuoYaoViewBookConfig> suoYaoViewBookConfigMap = SuoYaoBusiness.getSuoYaoViewBookConfigMap(tmpActivityInfo.getActivityId());
        tmpConfig.setViewBookConfigMap(suoYaoViewBookConfigMap);

        if (activityConfigMap.containsKey(eActivityConfigType.QUICK_USE_NUM.getName())) {
            tmpConfig.setQuickUseNum(activityConfigMap.get(eActivityConfigType.QUICK_USE_NUM.getName()).getIntValue());
        }

        return tmpConfig;
    }

    /**
     * 重新加载配置
     */
    public void reloadConfig() {
        int activityId = suoYaoConfig.getActivityId();
        ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
        suoYaoConfig = initConfig(activityInfo);
    }

    /**
     * 热更
     */
    public void repair() {

    }

    /**
     * 获取公告
     */
    public List<SuoYaoNotice> getSuoYaoNoticeData() {
        int maxCount = suoYaoConfig.getNoticeCount();
        List<SuoYaoNotice> tmpList = new ArrayList<>(suoYaoNoticeList);
        if (tmpList.size() > maxCount) {
            return tmpList.subList(tmpList.size() - maxCount, tmpList.size());
        } else {
            return tmpList;
        }
    }
}
